package cn.lili.modules.promotion.serviceimpl;

import cn.lili.modules.promotion.entity.dos.TicketDown;
import cn.lili.modules.promotion.mapper.TicketDownMapper;
import cn.lili.modules.promotion.service.TicketDownService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/14 15:44
 */
@Service
@RequiredArgsConstructor
public class TicketDownServiceImpl extends ServiceImpl<TicketDownMapper, TicketDown> implements TicketDownService {

    @Override
    public Boolean addTicketDown(TicketDown ticketDown) {
        Assert.notNull(ticketDown.getCard(), "身份证不能为空");
        Assert.notNull(ticketDown.getOpenId(), "openid不能为空");
        //检测是否领取门票
        Boolean flag = checkTicketDown(ticketDown.getOpenId());
        Assert.isTrue(!flag, "已领取门票，不能重复领取");
        return this.save(ticketDown);
    }

    @Override
    public Boolean checkTicketDown(String openId) {
        LambdaQueryWrapper<TicketDown> lambdaQueryWrapper = new LambdaQueryWrapper<TicketDown>();
        lambdaQueryWrapper.eq(TicketDown::getOpenId, openId);
        List<TicketDown> resultList = this.list(lambdaQueryWrapper);
        return !resultList.isEmpty();
    }
}
